package com.lmq.interfaces;// interfaces/AdaptedRandomDoubles.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Creating an adapter with inheritance
import java.nio.*;
import java.util.*;

public class AdaptedRandomDoubles
implements RandomDoubles, Readable {
  private int count;
  public AdaptedRandomDoubles(int count) {
    this.count = count;
  }
  @Override public int read(CharBuffer cb) {
    if(count-- == 0)
      return -1;
    String result = Double.toString(next()) + " ";
    cb.append(result);
    return result.length();
  }
  public static void main(String[] args) {
    Scanner s =
      new Scanner(new AdaptedRandomDoubles(7));
    while(s.hasNextDouble())
      System.out.print(s.nextDouble() + " ");
  }
}
/* Output:
0.7271157860730044 0.5309454508634242
0.16020656493302599 0.18847866977771732
0.5166020801268457 0.2678662084200585
0.2613610344283964
*/
/**
 * Because you can add an interface onto any existing class in this
 * way, it means that a method that takes an interface provides a way
 * to adapt any class to work with that method.This is the power of
 * using interfaces instead of classes.
 * Fields in interfaces
 * Because any fields you put into an interface are automatically
 * static and final, interface is a convenient tool for
 * creating groups of constant values.Before java 5, this was the only way
 * to produce the same effect as an enum in C or C++.So you will
 * see pre-java 5 code like this:
 * {{{
 *     public interface Months{
 *         int
 *         JANUARY = 1, FEBRUARY = 2,...;
 *     }
 *
 * }}}
 *
 * Notice the java style of using all uppercase letters(with underscores to seprate
 * multiple words in a single identifier) for static finals that have constant initializers.
 * The fields in an interface are automatically public, so that is not explicitly specified.
 * Since java5, we have the much more powerful and flexible enum keyword, so there
 * are
 * times you'll want to use that instead of interfaces to hold groups of
 * constants.
 * You can find more details on enums in the Enumerations chapters.
 *
 * initializing Filds in interfaces
 * Fileds defined in interfaces cannot be 'blank finals", but
 * they can be initialized with non-constant expressions.For
 * example:
 *
 */